package com.sinodata.bsm.cicp.finder.hp;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

/**
 * HP-UX卷组发现程序
 */
public class VolumeGroup extends HPFinder {
    private static final Logger logger = Logger.getLogger(VolumeGroup.class);
    private static final String SHELL = "vgdisplay";

    /**
     * 把同属一个卷组的多行数据组合到一个记录中
     * @param line
     * @param record
     */
    private void parseLine(String line, VGRecord record) {
        int pos = line.lastIndexOf(' ');
        String value = line.substring(pos + 1);
        if (line.startsWith("VG Name")) {
            record.name = value;
        } else if (line.startsWith("VG Write Access")) {
            record.access = value;
        } else if (line.startsWith("VG Status")) {
            record.status = value;
        } else if (line.startsWith("PE Size")) {
            record.peSize = value;
        } else if (line.startsWith("Total PE")) {
            record.peCount = value;
            record.vgSize = "" + Integer.parseInt(record.peSize) * Integer.parseInt(record.peCount);
        }
    }

    private static class VGRecord {
        private String name; //卷组名称
        private String access; //读写权限
        private String status; //状态
        private String peCount; //物理分区数
        private String peSize; //物理分区大小
        private String vgSize; //卷组大小
    }

    public static void main(String[] args) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] vgInfo = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            VGRecord currentRecord = null;
            List<VGRecord> vgList = new ArrayList<VGRecord>();
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            while ((strLine = reader.readLine()) != null) {
                strLine = strLine.trim();
                if (strLine.indexOf("VG Name") != -1) {//开始一个新的卷组
                    currentRecord = new VGRecord();
                    vgList.add(currentRecord);
                }
                if (currentRecord != null) {
                    parseLine(strLine, currentRecord); //把这行的数据放到当前记录中
                }
            }
            if (vgList.size() > 0) {
                for (VGRecord vg : vgList) {
                    vgInfo = new String[9];
                    list.add(vgInfo);
                    vgInfo[0] = vg.name;
                    vgInfo[1] = " volume group " + vg.name;
                    vgInfo[2] = getHostName(ip) + " volume group " + vg.name;
                    vgInfo[3] = vg.peCount; //物理分区大小
                    vgInfo[4] = vg.access; //访问权限
                    vgInfo[5] = vg.peCount; //物理分区数量
                    vgInfo[6] = vg.status; //卷组状态
                    vgInfo[7] = vg.vgSize; //总物理分区大小
                    vgInfo[8] = vg.name; //卷组名称
                }
            }
            return list;
        } catch (IOException e) {
            logger.error("failed to find HP VolumeGroup", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
